import Router from '@/router/index'
import Store from '@/store/index'
import { SettingOutlined, AppstoreOutlined } from '@ant-design/icons-vue';
import { h } from 'vue'



export const setDynamicMenu = () => {

    let allRouters = []
    const deepGetRouterItem = (item) => {
        if(item.children && item.children.length) {
            item.children.forEach(deepGetRouterItem)
        } else {
            allRouters.push(item)
        }
    }
    Router.options.routes.forEach(deepGetRouterItem);

    console.log(allRouters, 'allRouters');

    let powerList = Store.state.control.powerList
    let newMenu = []
    if(powerList.length) {
        powerList.forEach(item => {
            if ( item.menuFlag == '01' && item.subs && item.subs.length ) {
                let parMenuItem = {
                    icon: h(AppstoreOutlined),
                    label: item.perName,
                    key: item.menuCode || Math.random().toString(32).slice(2),
                };

                parMenuItem.children = [];
                item.subs.forEach((i) => {
                    if ( i.menuCode ) {
                        let routerItem = allRouters.find( (m) => m.meta && m.meta.menuCode == i.menuCode );
                        if (routerItem) {
                            routerItem.label = i.perName;
                            parMenuItem.children.push({
                                label: i.perName,
                                key: i.menuCode || Math.random().toString(32).slice(2),
                                path: routerItem.path,
                            });
                        }
                    }
                });
                newMenu.push(parMenuItem);
            }
        })
    }
    console.log(newMenu, 'newMenu');
    return newMenu

}
